<?php

namespace App\Http\Requests\Admin\AreaStation;
use App\Http\Requests\Request;
use App\Models\Admin\AreaStation;
use Validator;

class UpdateRequest extends Request{
    public function authorize(){
        return true;
    }
    public function rules(){
        //匹配域名
        Validator::extend('check_domain', function($attribute, $value, $parameters) {
            $preg="/^(https?:\/\/)((\w)+\.)?(.*?)\.(\w)([\w-\.\-\/\?\&|%\+]*)?/Uis";
            if(preg_match_all($preg, $value, $matches)){
                return true;
            }else{
                return false;
            }
        });
        //添加编码排序判断
        Validator::extend('check_station_sort', function($attribute, $value, $parameters){
            if($value != 0){
                $AreaStationSort = AreaStation::where('id','!=', $this->id)->pluck('station_sort')->toArray();
                if(in_array($value, $AreaStationSort)){
                    return false;
                }else{
                    return true;
                }
            }else{
                return true;
            }
        });
        return [
            'station_domain' => 'required|check_domain|unique:area_station,station_domain,'.$this->id,
            'station_sign' => 'required|unique:area_station,station_sign,'.$this->id,
            'station_name' => 'required',
            'province' => 'required',
            'area' => 'required',
            'city' => 'required',
            'station_cid' => 'required|unique:area_station,station_cid,'.$this->id,
            'station_title' => 'required',
            'station_keyword' => 'required',
            'station_description' => 'required',
            'station_sort' => 'integer|check_station_sort',
        ];
    }
    public function messages(){
        return [
            'station_domain.required' => '分站域名必须填写',
            'station_domain.unique' => '分站域名必须是唯一',
            'station_domain.check_domain' => '分站域名不正确',
            'station_sign.required' => '分站标识必须填写',
            'station_sign.unique' => '分站标识必须是唯一',
            'station_name.required' => '分站名称必须填写',
            'province.required' => '省份必须选择',
            'area.required' => '地级市必须选择',
            'city.required' => '县/市/区必须选择',
            'station_cid.required' => '分站编码不能为空',
            'station_cid.unique' => '分站编码必须是唯一',
            'station_title.required' => '分站标题必须填写',
            'station_keyword.required' => '分站关键词必须填写',
            'station_description.required' => '分站描述必须填写',
            'station_sort.integer' => '分站排序必须是整数',
            'station_sort.check_station_sort' => '分站排序不能相同'
        ];
    }
}